package com.job.service.impl;

import java.math.BigDecimal;
import java.util.Set;

import javax.transaction.Transactional;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.job.common.service.BaseServiceImpl;
import com.job.dao.SorderDao;
import com.job.model.Forder;
import com.job.model.Product;
import com.job.model.Sorder;
import com.job.model.User;
import com.job.service.SorderService;

@Transactional
@Service
public class SorderServiceImpl extends BaseServiceImpl<Sorder> implements SorderService {
	@Autowired
	private SorderDao sorderDao;

	@Override
	public void save(Product product, User user) {
		Set<Forder> forders = user.getForders();
		Sorder sorder = new Sorder();
		sorder.setName(product.getName());
		sorder.setNumber(1);
		sorder.setPrice(product.getPrice());
		sorder.setProduct(product);

		for (Forder forder : forders) {
			sorder.setForder(forder);
			break;
		}
		sorderDao.add(sorder);

		
		BigDecimal b = new BigDecimal(0);

		for (Forder forder : forders) {
			b = sorder.getPrice().add(forder.getTotal());
			forder.setTotal(b);
		}
		
	}

}
